﻿using System.Diagnostics.Contracts;

namespace O1.Kernel.IO.KeyGeneration
{
    [ContractVerification(true)]
    internal class DurableCounter : SequenceProvider<int>
    {
        private int value;

        public DurableCounter(int initialValue)
        {
            Contract.Requires(initialValue > -1);
            this.value = initialValue;
        }

        public override int Reserve(int rangeSize)
        {
            Contract.Assume(rangeSize > 0);

            var current = this.value;
            this.value += rangeSize;
            return current;
        }

        [ContractInvariantMethod]
        private void ObjectInvariant()
        {
            Contract.Invariant(this.value > -1);
        }
    }
}
